package org.bminfo.migle.parser;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.bminfo.migle.filemanage.FileProducer;

public class Initialization {
	
	public String miGLE_home = "";
	public String moopPath = "";
	public String tempRoot = "";
	
	public void setmiGLEHome(String path){
		miGLE_home = path.replace("\\", "/");
		moopPath = miGLE_home + "WEB-INF/classes/org/bminfo/migle/miGLE/";
		tempRoot = miGLE_home + "temp/";
	}
	
	String folderName;
	String tempPath;

	public String initialization(String filename, String filecontent) throws IOException{
	
		FileProducer fileProducer = new FileProducer();
		folderName = fileProducer.folderNameProducer();
		
		System.out.println("Creating the foldername:" + folderName);
		
//		System.out.println(miGLE_home);
//		System.out.println(moopPath);
//		System.out.println(tempRoot);
		
		tempPath = tempRoot + folderName + "/";
		
		File folder = new File(tempPath);
		if (folder.mkdir() == false){
			folder.mkdir();
			System.out.println("make dir");
		}
		
		File gbfile = new File(tempPath + filename);
		PrintWriter wr = new PrintWriter(new BufferedWriter(new FileWriter(gbfile)));
		wr.write(filecontent);
		wr.close();
		
		File htmlfolder = new File(tempPath + "list/");
		System.out.println(tempPath + "list/");
		if (htmlfolder.mkdir() == false){
			htmlfolder.mkdir();
			System.out.println("make dir");
		}
		
		return folderName;
	}
}
